﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Protogamez
{
    /// <summary>
    /// Interaction logic for UserTimeDialog.xaml
    /// </summary>
    public partial class UserTimeDialog : Window
    {
        /// <summary>
        /// SET REFERENCIAL VARIABLE
        /// </summary>
        OleDbConnection conn = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;data source=" + AppDomain.CurrentDomain.BaseDirectory + "\\Resources\\gamesdb.mdb");

        string gamenamepass = "";
        string sqlcommand = "";

        DateTime starttime = DateTime.Now;
        TimeSpan timespan;

        /// <summary>
        /// SET GLOBAL FUNCTION
        /// </summary>
        private void games_time_update(string sqlcommand)
        {
            conn.Open();
            OleDbCommand cmd = conn.CreateCommand();
            cmd.CommandText = sqlcommand;
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        /// <summary>
        /// GLOBAL SOURCE CODE
        /// </summary>
        public UserTimeDialog(string gamename)
        {
            InitializeComponent();

            DispatcherTimer timer = new DispatcherTimer(new TimeSpan(0, 0, 1), DispatcherPriority.Normal, delegate
            {
                DateTime stoptime = DateTime.Now;
                timespan = stoptime - starttime;
                this.label_playedresult.Content = new DateTime(timespan.Ticks).ToString("HH:mm:ss");
            }, this.Dispatcher);

            gamenamepass = gamename;
        }

        private void windows_playtime_Loaded(object sender, RoutedEventArgs e)
        {
            label_gamename.Content = gamenamepass;
        }

        private void button_confexit_Click(object sender, RoutedEventArgs e)
        {
            DateTime stoptime = DateTime.Now;
            TimeSpan timespan = stoptime - starttime;

            DateTime.Now.ToShortDateString();

            string timeplayedpass1 = new DateTime(timespan.Ticks).ToString("HH"); 
            string timeplayedpass2 = new DateTime(timespan.Ticks).ToString("mm");
            
            int timeplayedpass1_1 = Convert.ToInt32(timeplayedpass1) * 60;

            int timeplayedpass = timeplayedpass1_1 + Convert.ToInt32(timeplayedpass2);

            sqlcommand = @"INSERT INTO timetrack([gamelink], [sessiontime])VALUES('" + gamenamepass + "', '" + timeplayedpass + "')";
            games_time_update(sqlcommand);
            sqlcommand = @"UPDATE gamecentral SET lastplayed='" + DateTime.Now.ToString("D") + "' WHERE gamename = '" + gamenamepass + "'";
            games_time_update(sqlcommand);

            this.Close();
        }

        private void windows_playtime_GotFocus(object sender, RoutedEventArgs e)
        {
            this.label_playedresult.Content = new DateTime(timespan.Ticks).ToString("HH:mm:ss");
        }
    }
}
